package com.erdos.upc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.upc.dao.GroupDao;
import com.erdos.upc.dao.PrivilegeDao;
import com.erdos.upc.dao.RoleDao;
import com.erdos.upc.entity.Privilege;
import com.erdos.upc.entity.Role;
import com.erdos.upc.service.RoleService;

@SuppressWarnings("serial")
public class RoleServiceImpl extends BaseCRUDService<Role> implements
		RoleService {

    private static final Log logger = LogFactory.getLog(RoleServiceImpl.class);
	private RoleDao roleDao;
	private GroupDao groupDao;
	private PrivilegeDao privilegeDao;
	
	
	public PrivilegeDao getPrivilegeDao() {
		return privilegeDao;
	}
	public void setPrivilegeDao(PrivilegeDao privilegeDao) {
		this.privilegeDao = privilegeDao;
	}
	/**
	 * 完成创建角色,同时给角色授权
	 */
	
	public Long insert(Role role){
		logger.debug("insert user start!");
		Long sid=super.insert(role);
		if (role.getPrivileges() != null && role.getPrivileges().size() != 0) {
			for (Privilege p : role.getPrivileges()) {
				Privilege privilege = this.privilegeDao.findByPrimaryKey(p.getSid());
				this.getRoleDao().insertRolePrivilege(role, privilege.getSid());
			}
		}
		return sid;
	}
	/**
	 * 完成删除角色，并删除相应的关联关系
	 */
	
	public int delete(Role role){
		roleDao.deleteRoleUserGroup(role.getSid());
		roleDao.deleteRolePrivilege(role.getSid());
		return roleDao.delete(role);
		
	}
	
	/**
	 * 完成更新角色，并更新相应的权限
	 */
	
	public void update(Role role){
	roleDao.update(role);
	roleDao.deleteRolePrivilege(role.getSid());
	if (role.getPrivileges() != null && role.getPrivileges().size() != 0) {
		for (Privilege p : role.getPrivileges()) {
			Privilege privilege = this.privilegeDao.findByPrimaryKey(p.getSid());
			this.getRoleDao().insertRolePrivilege(role, privilege.getSid());
		}
	}

		
	}

	public List<Role> findByPrivilegeCode(String privilegeCode) {
        return roleDao.findByPrivilegeCode(privilegeCode);
    }

	 public void insertRolePrivilege(Role role, Long privilegeSid) {
	        roleDao.insertRolePrivilege(role, privilegeSid);
	        
	    }

	public RoleDao getRoleDao() {
		return roleDao;
	}



	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.service.RoleService#checkName()
	 */
	public boolean checkName(String sid,String name) {
		boolean flag;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		Role role = getRoleDao().findUniqueByParams(params);
		
		if (StringUtils.isEmpty(sid)) {
			flag=( role == null ? false : true);
		} else {
			if (role==null)
				flag= false;
			else if (sid.equals(role.getSid().toString())) {
				flag= false;
			} else {
				flag= true;
			}
		}
		return flag;
	}
}


